home *** CD-ROM | disk | FTP | other *** search
- /* ---------------- menubar.c -------------- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <dos.h>
- #include "dflat.h"
-
- static void reset_menubar(WINDOW);
- static int TestGlobalKeys(WINDOW, PARAM, PARAM);
-
- static struct {
- int x1, x2; /* position in menu bar */
- char sc; /* shortcut key value */
- } menu[10];
- static int mctr;
-
- MBAR *ActiveMenuBar;
- static MENU *ActiveMenu;
- WINDOW MenuBarWnd;
-
- static WINDOW mwnd;
- static int Selecting;
-
- static WINDOW Cascaders[MAXCASCADES];
- static int casc;
-
- int MenuBarProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
- {
- int offset = 3, wd, offset1, i, j, mx, my;
- MENU *mnu;
- int rtn;
- static int AltDown = FALSE;
-
- switch (msg) {
- case CREATE_WINDOW:
- MenuBarWnd = wnd;
- reset_menubar(wnd);
- break;
- case SETFOCUS:
- rtn = BaseWndProc(MENUBAR, wnd, msg, p1, p2);
- if ((int) p1) {
- if (ActiveMenuBar->ActiveSelection == -1)
- ActiveMenuBar->ActiveSelection = 0;
- if (inFocus == wnd)
- SendMessage(wnd, PAINT, 0, 0);
- }
- else {
- SendMessage(wnd, PAINT, 0, 0);
- #ifdef INCLUDE_STATUSBAR
- SendMessage(GetParent(wnd), ADDSTATUS, 0, 0);
- #endif
- }
- return rtn;
- case COMMAND:
- if (isCascadedCommand(ActiveMenuBar, (int)p1)) {
- /* find the cascaded menu based on command id in p1 */
- mnu = ActiveMenu+mctr;
- while (mnu->Title != (void *)-1) {
- if (mnu->CascadeId == (int) p1) {
- if (casc < MAXCASCADES) {
- Cascaders[casc++] = mwnd;
- SendMessage(wnd, SELECTION,
- (PARAM)(mnu-ActiveMenu), TRUE);
- }
- break;
- }
- mnu++;
- }
- }
- else {
- if (mwnd != NULLWND)
- SendMessage(mwnd, CLOSE_WINDOW, 0, 0);
- SetPrevFocus(wnd);
- PostMessage(GetParent(wnd), msg, p1, p2);
- }
- return TRUE;
- case BUILDMENU:
- reset_menubar(wnd);
- mctr = 0;
- ActiveMenuBar = (MBAR *) p1;
- ActiveMenu = ActiveMenuBar->PullDown;
- while (ActiveMenu->Title != NULL) {
- char *cp;
- if (strlen(GetText(wnd)+offset) < strlen(ActiveMenu->Title)+3)
- break;
- GetText(wnd) = realloc(GetText(wnd), strlen(GetText(wnd))+5);
- memmove(GetText(wnd) + offset+4, GetText(wnd) + offset,
- strlen(GetText(wnd))-offset+1);
- CopyCommand(GetText(wnd)+offset, ActiveMenu->Title, FALSE,
- wnd->WindowColors [STD_COLOR] [BG]);
- menu[mctr].x1 = offset;
- offset += strlen(ActiveMenu->Title) + (3+MSPACE);
- menu[mctr].x2 = offset-MSPACE;
- cp = strchr(ActiveMenu->Title, SHORTCUTCHAR);
- if (cp)
- menu[mctr].sc = tolower(*(cp+1));
- mctr++;
- ActiveMenu++;
- }
- ActiveMenu = ActiveMenuBar->PullDown;
- break;
- case PAINT:
- if (!isVisible(wnd) || GetText(wnd) == NULL)
- break;
-
- #ifdef INCLUDE_STATUSBAR
- if (wnd == inFocus)
- SendMessage(GetParent(wnd), ADDSTATUS, 0, 0);
- #endif
- SetStandardColor(wnd);
- ClearAttribute(wnd, NOCLIP);
-
- clipline(wnd, 0, GetText(wnd));
- wputs(wnd, GetText(wnd), 0, 0);
-
- AddAttribute(wnd, NOCLIP);
- if (ActiveMenuBar->ActiveSelection != -1 &&
- (wnd == inFocus || mwnd != NULLWND)) {
- char *sel;
- char *cp;
- if ((sel = malloc(200)) != NULL) {
- offset = menu[ActiveMenuBar->ActiveSelection].x1;
- offset1 = menu[ActiveMenuBar->ActiveSelection].x2;
- GetText(wnd)[offset1] = '\0';
- SetReverseColor(wnd);
- memset(sel, '\0', 200);
- strcpy(sel, GetText(wnd)+offset);
- cp = strchr(sel, CHANGECOLOR);
- if (cp != NULL)
- *(cp + 2) = background | 0x80;
- wputs(wnd, sel, offset-ActiveMenuBar->ActiveSelection*4, 0);
- GetText(wnd)[offset1] = ' ';
- #ifdef INCLUDE_STATUSBAR
- if (!Selecting && mwnd == NULLWND && wnd == inFocus) {
- char *st = ActiveMenu[ActiveMenuBar->ActiveSelection].StatusText;
- if (st != NULL)
- SendMessage(GetParent(wnd), ADDSTATUS, (PARAM)st, 0);
- }
- #endif
- free(sel);
- }
- }
- return FALSE;
- case SHIFT_CHANGED:
- if (mwnd == NULLWND) {
- if ((int)p1 & ALTKEY)
- AltDown = TRUE;
- else if (AltDown)
- SendMessage(wnd, KEYBOARD,
- wnd == inFocus ? ESC : F10, 0);
- }
- return TRUE;
- case KEYBOARD:
- AltDown = FALSE;
- if (mwnd == NULLWND) {
- /* ----- search for menu bar shortcut keys ---- */
- int c = tolower((int)p1);
- int a = AltConvert((int)p1);
- for (j = 0; j < mctr; j++) {
- if ((inFocus == wnd && menu[j].sc == c) ||
- (a && menu[j].sc == a)) {
- if (inFocus != wnd)
- SendMessage(wnd, SETFOCUS, TRUE, 0);
- SendMessage(wnd, SELECTION, j, 0);
- return FALSE;
- }
- }
- }
- /* -------- search for accelerator keys -------- */
- mnu = ActiveMenu;
- while (mnu->Title != NULL) {
- struct PopDown *pd = mnu->Selections;
- if (mnu->PrepMenu)
- (*(mnu->PrepMenu))(GetParent(wnd), mnu);
- while (pd->SelectionTitle != NULL) {
- if (pd->Accelerator == (int) p1) {
- if (pd->Attrib & INACTIVE)
- beep();
- else {
- if (GetClass(inFocus) == MENUBAR)
- SetPrevFocus(inFocus);
- PostMessage(GetParent(wnd),
- COMMAND, pd->ActionId, 0);
- }
- return TRUE;
- }
- pd++;
- }
- mnu++;
- }
- if (TestGlobalKeys(wnd, p1, p2))
- return TRUE;
-
- switch ((int)p1) {
- #ifdef INCLUDE_HELP
- case F1:
- if (ActiveMenu != NULL &&
- (mwnd == NULLWND ||
- (ActiveMenu+ActiveMenuBar->ActiveSelection)->
- Selections[0].SelectionTitle == NULL)) {
- DisplayHelp(wnd, (ActiveMenu+ActiveMenuBar->ActiveSelection)->Title+1);
- return TRUE;
- }
- break;
- #endif
- case '\r':
- if (mwnd == NULLWND && ActiveMenuBar->ActiveSelection != -1)
- SendMessage(wnd, SELECTION, ActiveMenuBar->ActiveSelection, 0);
- break;
- case ESC:
- if (inFocus == wnd) {
- ActiveMenuBar->ActiveSelection = -1;
- SetPrevFocus(wnd);
- SendMessage(wnd, PAINT, 0, 0);
- }
- break;
- case FWD:
- ActiveMenuBar->ActiveSelection++;
- if (ActiveMenuBar->ActiveSelection == mctr)
- ActiveMenuBar->ActiveSelection = 0;
- if (mwnd != NULLWND)
- SendMessage(wnd, SELECTION, ActiveMenuBar->ActiveSelection, 0);
- else
- SendMessage(wnd, PAINT, 0, 0);
- break;
- case BS:
- if (ActiveMenuBar->ActiveSelection == 0)
- ActiveMenuBar->ActiveSelection = mctr;
- --ActiveMenuBar->ActiveSelection;
- if (mwnd != NULLWND)
- SendMessage(wnd, SELECTION, ActiveMenuBar->ActiveSelection, 0);
- else
- SendMessage(wnd, PAINT, 0, 0);
- break;
- default:
- break;
- }
- return FALSE;
- case LEFT_BUTTON:
- i = BarSelection((int) p1 - GetLeft(wnd));
- if (i < mctr)
- if (i != ActiveMenuBar->ActiveSelection || mwnd == NULLWND)
- SendMessage(wnd, SELECTION, i, 0);
- return TRUE;
- case SELECTION:
- Selecting = TRUE;
- mnu = ActiveMenu+(int)p1;
- if (mnu->PrepMenu != NULL)
- (*(mnu->PrepMenu))(GetParent(wnd), mnu);
- wd = MenuWidth(mnu->Selections);
- if (p2) {
- mx = GetLeft(inFocus) + WindowWidth(inFocus) - 1;
- my = GetTop(inFocus) + inFocus->selection;
- }
- else {
- if (mwnd != NULLWND)
- SendMessage(mwnd, CLOSE_WINDOW, 0, 0);
- ActiveMenuBar->ActiveSelection = (int) p1;
- offset = menu[ActiveMenuBar->ActiveSelection].x1 -
- 4 * ActiveMenuBar->ActiveSelection;
- if (offset > WindowWidth(wnd)-wd)
- offset = WindowWidth(wnd)-wd;
- mx = GetLeft(wnd)+offset;
- my = GetTop(wnd)+1;
- }
- mwnd = CreateWindow(POPDOWNMENU, NULL,
- mx, my,
- MenuHeight(mnu->Selections),
- wd,
- NULL,
- wnd,
- NULL,
- 0);
- #ifdef INCLUDE_SHADOWS
- AddAttribute(mwnd, SHADOW);
- #endif
- if (mnu->Selections[0].SelectionTitle != NULL) {
- SendMessage(mwnd, BUILD_SELECTIONS, (PARAM) mnu, 0);
- SendMessage(mwnd, SETFOCUS, TRUE, 0);
- }
- else
- SendMessage(wnd, PAINT, 0, 0);
- Selecting = FALSE;
- break;
- case BORDER:
- return TRUE;
- case INSIDE_WINDOW:
- return InsideRect(p1, p2, WindowRect(wnd));
- case CLOSE_POPDOWN:
- if (casc > 0)
- SendMessage(Cascaders[--casc], CLOSE_WINDOW, p1, 0);
- else {
- if ((int)p1)
- PostMessage(wnd, KEYBOARD, ESC, 0);
- mwnd = NULLWND;
- ActiveMenuBar->ActiveSelection = -1;
- }
- return TRUE;
- case CLOSE_WINDOW:
- rtn = BaseWndProc(MENUBAR, wnd, msg, p1, p2);
- if (GetText(wnd) != NULL) {
- free(GetText(wnd));
- GetText(wnd) = NULL;
- }
- mctr = 0;
- ActiveMenuBar->ActiveSelection = -1;
- MenuBarWnd = NULL;
- ActiveMenu = NULL;
- ActiveMenuBar = NULL;
- return rtn;
- default:
- break;
- }
- return BaseWndProc(MENUBAR, wnd, msg, p1, p2);
- }
-
- static void reset_menubar(WINDOW wnd)
- {
- if ((GetText(wnd) = realloc(GetText(wnd), SCREENWIDTH+5)) != NULL) {
- memset(GetText(wnd), ' ', SCREENWIDTH);
- *(GetText(wnd)+SCREENWIDTH-
- (TestAttribute(GetParent(wnd), HASBORDER) ? 2 : 0)) = '\0';
- }
- }
-
- static int TestGlobalKeys(WINDOW wnd, PARAM p1, PARAM p2)
- {
- switch ((int)p1) {
- case F10:
- if (MenuBarWnd != NULLWND)
- SendMessage(MenuBarWnd, SETFOCUS, TRUE, 0);
- return TRUE;
- case ALT_F6:
- if (GetClass(inFocus) == POPDOWNMENU)
- return TRUE;
- SetNextFocus(inFocus);
- SkipSystemWindows(FALSE);
- return TRUE;
- #ifdef INCLUDE_SYSTEM_MENUS
- case ALT_HYPHEN:
- if (GetClass(inFocus) == POPDOWNMENU)
- SendMessage(inFocus, CLOSE_WINDOW, 0, 0);
- if (GetClass(GetParent(inFocus)) == APPLICATION)
- BuildSystemMenu(GetParent(inFocus));
- return TRUE;
- case ' ':
- if ((int)p2 & ALTKEY) {
- if (GetClass(inFocus) == POPDOWNMENU)
- SendMessage(inFocus, CLOSE_WINDOW, 0, 0);
- if (GetClass(inFocus) != MENUBAR &&
- TestAttribute(inFocus, HASTITLEBAR) &&
- TestAttribute(inFocus, CONTROLBOX))
- BuildSystemMenu(inFocus);
- return TRUE;
- }
- break;
- #endif
- case CTRL_F4:
- if (GetClass(inFocus) != MENUBAR)
- #ifdef INCLUDE_STATUSBAR
- if (GetClass(inFocus) != STATUSBAR)
- #endif
- if (GetClass(inFocus) != APPLICATION) {
- SendMessage(inFocus, CLOSE_WINDOW, 0, 0);
- SkipSystemWindows(FALSE);
- }
- break;
- case ALT_F4:
- PostMessage(GetParent(wnd), CLOSE_WINDOW, 0, 0);
- break;
- default:
- break;
- }
- return FALSE;
- }
-
- int BarSelection(int mx)
- {
- int i;
- for (i = 0; i < mctr; i++)
- if (mx >= menu[i].x1-4*i &&
- mx <= menu[i].x2-4*i-5)
- break;
- return i;
- }
-
-